package netWork.练习1_聊天室.多线程实现;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Package netWork.练习1_聊天室.多线程实现
 * @Author shimmer
 * @Date 2020/8/2 14:06
 * @TODO
 */
public class ReceiveThread implements Runnable {
    //获取socket对象
    Socket socket = null;
    DataInputStream is = null;
    Scanner scanner = null;

    public ReceiveThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            is = new DataInputStream(socket.getInputStream());
            scanner = new Scanner(System.in);
            while (true) {
                System.out.println(is.readUTF());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (scanner != null) {
                scanner.close();
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
